package com.xpgk.mvc.statistic.monthSales;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Service;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.mvc.statistic.dailyNursing.DailyNursingStatisticService;
import com.xpgk.mvc.statistic.daySales.DaySalesStatisticSQL;
import com.xpgk.tools.ToolDateTime;


@Service(name = MonthSalesStatisticService.serviceName)
public class MonthSalesStatisticService extends  BaseService{
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(MonthSalesStatisticService.class);
	public static final String serviceName = "monthSalesStatisticService";
	
	
	
	public  List<Record>  getView(String date,String organIds){
			List<Record> record = new ArrayList<>();
			if(!"-1".equals(date) && !"-1".equals(organIds)){
				int hjpeopleNum=0;//合计新增人数
				int hjlcs=0;//合计疗程数
				int hjdlcrs=0;//合计单疗程人数
				double hjdlcje=0;//合计单疗程金额
				int hjbzlcrs=0;//合计标准疗程人数
				int hjbzlclcs=0;//合计标准疗程疗程数
				double hjbzlcje=0;//合计标准疗程金额
				int hjzlcrs=0;//合计转疗程人数
				int hjzlclcs=0;//合计转疗程疗程数
				double hjzlcje=0;//合计转疗程金额
				double hjmrjzje=0;//合计每日减重金额
				double hjmrproje=0;//合计每日产品金额
				String sql_product = getSqlMy(DaySalesStatisticSQL.sqlId_product);
				List<Record> products = Db.find(sql_product, Code.lc_ids, Code.proYt_ids);
				List<String> days = ToolDateTime.getMonthAllDays(date);
				if(days.size() > 0){
					
					for(String day : days){
						Record r = new Record();
						double lctotalprice=0;//每日减重 金额
						double producttotalprice=0;//每日产品金额
						//日期
						r.set("date", day);
						
						//新增人数
						String sql_newPeopleNum = getSqlMy(MonthSalesStatisticSQL.sqlId_newPeopleNum);
						Record newPeopleNum = Db.findFirst(sql_newPeopleNum, day, day, Code.lc_ids, organIds, day);
						if(null != newPeopleNum){
							r.set("newPeopleNum", newPeopleNum.getLong("newPeopleNum"));
							hjpeopleNum+=newPeopleNum.getLong("newPeopleNum")==null?0:newPeopleNum.getLong("newPeopleNum");
						}
						
						//疗程数
						String sql_newLcNum = getSqlMy(MonthSalesStatisticSQL.sqlId_newLcNum);
						Record newLcNum = Db.findFirst(sql_newLcNum, day, day, Code.lc_ids, Code.bzlc_units, organIds, day);
						if(null != newLcNum){
							r.set("newLcNum", newLcNum.getBigDecimal("newLcNum"));
							hjlcs+=newLcNum.getBigDecimal("newLcNum")==null?0:Integer.parseInt(newLcNum.getBigDecimal("newLcNum").toString());
						}
						
						//单疗程
						String sql_singleLc = getSqlMy(MonthSalesStatisticSQL.sqlId_lc);
						Record singleLc = Db.findFirst(sql_singleLc, day, Code.lc_ids, Code.dlc_units, organIds, day);
						if(null != singleLc){
							r.set("singleLc_peopleNum", singleLc.getLong("peopleNum"));
							r.set("singleLc_unitPrice", singleLc.getFloat("unitPrice"));
							r.set("singleLc_totalPrice", singleLc.getDouble("totalPrice"));
							lctotalprice+=singleLc.getDouble("totalPrice")==null?0:singleLc.getDouble("totalPrice");
							hjdlcrs+=singleLc.getLong("peopleNum")==null?0:singleLc.getLong("peopleNum");
							hjdlcje+=singleLc.getDouble("totalPrice")==null?0:singleLc.getDouble("totalPrice");
						}
						
						//标准疗程
						String sql_normLc = getSqlMy(MonthSalesStatisticSQL.sqlId_lc);
						Record normLc = Db.findFirst(sql_normLc, day, Code.lc_ids, Code.bzlc_units, organIds, day);
						if(null != normLc){
							r.set("normLc_peopleNum", normLc.getLong("peopleNum"));
							r.set("normLc_lcNum", normLc.getBigDecimal("lcNum"));
							r.set("normLc_unitPrice", normLc.getFloat("unitPrice"));
							r.set("normLc_totalPrice", normLc.getDouble("totalPrice"));
							lctotalprice+=normLc.getDouble("totalPrice")==null?0:normLc.getDouble("totalPrice");
							hjbzlcrs+=normLc.getLong("peopleNum")==null?0:normLc.getLong("peopleNum");
							hjbzlclcs+=normLc.getBigDecimal("lcNum")==null?0:Integer.parseInt(normLc.getBigDecimal("lcNum").toString());
							hjbzlcje+=normLc.getDouble("totalPrice")==null?0:normLc.getDouble("totalPrice");
						}
						
						//转疗程
						String sql_upLc = getSqlMy(MonthSalesStatisticSQL.sqlId_upLc);
						Record upLc = Db.findFirst(sql_upLc, day, Code.lc_ids, organIds, day);
						if(null != upLc){
							r.set("upLc_peopleNum", upLc.getLong("peopleNum"));
							r.set("upLc_lcNum", upLc.getBigDecimal("lcNum"));
							r.set("upLc_totalPrice", upLc.getDouble("totalPrice"));
							lctotalprice+=upLc.getDouble("totalPrice")==null?0:upLc.getDouble("totalPrice");
							hjzlcrs+=upLc.getLong("peopleNum")==null?0:upLc.getLong("peopleNum");
							hjzlclcs+=upLc.getBigDecimal("lcNum")==null?0:Integer.parseInt(upLc.getBigDecimal("lcNum").toString());
							hjzlcje+=upLc.getDouble("totalPrice")==null?0:upLc.getDouble("totalPrice");
						}
						
						r.set("lctotalprice", lctotalprice);
						//其他产品
						
						if(products.size() > 0){
//							Double sum = 0.0;
							for(Record product : products){
								String sql_productStatistic = getSqlMy(MonthSalesStatisticSQL.sqlId_productStatistic);
								Record productStatistic = Db.findFirst(sql_productStatistic, day, product.getStr("ids"), organIds, day);
								if(null != productStatistic){
									r.set(product.getStr("bh") + "_num", productStatistic.getBigDecimal("num"));
									r.set(product.getStr("bh") + "_price", productStatistic.getDouble("price"));
									producttotalprice += productStatistic.getDouble("price");
								}
							}
							r.set("producttotalprice", producttotalprice);
						}
						hjmrjzje+=lctotalprice;
						hjmrproje+=producttotalprice;
						r.set("sum", lctotalprice+producttotalprice);
						record.add(r);
						
						String curDate = ToolDateTime.format(new Date(), ToolDateTime.pattern_ymd);
						if(day.equals(curDate)){
							break;
						}
					}
					Record  hj=new Record();
					hj.set("date", "合计");
					hj.set("newpeoplenum", hjpeopleNum);
					hj.set("newlcnum", hjlcs);
					hj.set("singleLc_peopleNum", hjdlcrs);
					hj.set("singleLc_totalPrice", hjdlcje);
					hj.set("normLc_peopleNum", hjbzlcrs);
					hj.set("normLc_lcNum", hjbzlclcs);
					hj.set("normLc_totalPrice", hjbzlcje);
					hj.set("upLc_peopleNum", hjzlcrs);
					hj.set("upLc_lcNum", hjzlclcs);
					hj.set("upLc_totalPrice", hjzlcje);
					hj.set("lctotalprice", hjmrjzje);
					for(Record product:products)
					{	
						double  hjprosl=0;
						double  hjproje=0;
						for(int n=0;n<record.size();n++)
						{
							hjprosl+=record.get(n).getBigDecimal(product.getStr("bh") + "_num")==null?0:Double.parseDouble(record.get(n).getBigDecimal(product.getStr("bh") + "_num").toString());
							hjproje+=record.get(n).getDouble(product.getStr("bh") + "_price")==null?0:record.get(n).getDouble(product.getStr("bh") + "_price");
						}
						hj.set(product.getStr("bh") + "_num", hjprosl);
						hj.set(product.getStr("bh") + "_price", hjproje);
					}
					hj.set("producttotalprice", hjmrproje);
					hj.set("sum", hjmrjzje+hjmrproje);
					record.add(hj);
				}
			}
			
			 return  record;
		}
	
}
